package org.linlinjava.litemall.admin.service;

import org.linlinjava.litemall.admin.shiro.AuthUtils;
import org.linlinjava.litemall.db.dao.LitemallSeatDetailMapper;
import org.linlinjava.litemall.db.dao.LitemallSeatMapper;
import org.linlinjava.litemall.db.dao.SeatMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:	订座管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-10 16:20:45
 */
@Service
@Transactional
public class AdminSeatService {
    @Autowired
    private LitemallSeatMapper seatMapper;

    @Autowired
    private SeatMapper mySeatMapper;
    @Autowired
    private LitemallSeatDetailMapper seatDetailMapper;

    public LitemallSeat findById(Integer id) {
        return seatMapper.selectByPrimaryKey(id);
    }


    public void add(LitemallSeat entity) {
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleted(false);
        Integer storeId = AuthUtils.getStoreId();
        entity.setStoreId(storeId);
        seatMapper.insertSelective(entity);
    }

    public int updateById(LitemallSeat entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return seatMapper.updateByPrimaryKeySelective(entity);
    }

    public List<LitemallSeat> querySelective(String name,Integer page, Integer size, String sort, String order) {
        Integer storeId = AuthUtils.getStoreId();
        return mySeatMapper.selectByExample(name,storeId,sort,order);
    }

    public int count() {
        LitemallSeatExample example = new LitemallSeatExample();
        example.or().andDeletedEqualTo(false);

        return (int) seatMapper.countByExample(example);
    }

    public void deleteById(Integer id) {
        seatMapper.logicalDeleteByPrimaryKey(id);
    }

    public List<LitemallSeat> findSeat(String seatNo) {
        LitemallSeatExample example = new LitemallSeatExample();
        LitemallSeatExample.Criteria criteria = example.createCriteria();
        criteria.andSeatNoEqualTo(seatNo);
        Integer storeId = AuthUtils.getStoreId();
        if (storeId != null) {
            criteria.andStoreIdEqualTo(storeId);
        }
        return seatMapper.selectByExample(example);
    }


}
